<?php


namespace app\common\model;
use app\common\model\Station;
use think\Model;
use think\Db;
class Device extends Model
{
    protected $table = 'store_device';

    public function add($param){
        $find = self::where(['sn'=>$param['sn']])->find();
        if(empty($find)){
            $Station = new Station();
            $param['station_id'] = $Station::where(['user_id'=>$param['user_id']])->value("id");
            $param['create_at'] = date('Y-m-d H:i:s');
            $param['images'] = str_replace(',','|',$param['images']);
            $param['map_image'] = $param['images'];

            $param['sn'] = substr(date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT),0,14);
            unset($param['cost_model']);
            self::insert($param);
            return 1;
        }else{
            return '编号重复,请勿重复提交！';
        }
    }

    public function device_save($param){
        $user_id = $param['user_id'];
        $find = self::where(['sn'=>$param['sn']])->where("user_id!=$user_id")->find();
        if(empty($find)){
            $row = self::where(["id"=>$param['id']])->find();
            if($row){
                if($row['user_id']==$param['user_id']){
                    $Station = new Station();
                    $param['station_id'] = $Station::where(['user_id'=>$param['user_id']])->value("id");
                    $param['create_at'] = date('Y-m-d H:i:s');
                    $param['images'] = str_replace(',','|',$param['images']);
                    $param['map_image'] = $param['images'];

                    unset($param['cost_model']);

                    self::where(["id"=>$param['id']])->update($param);
                    return 1;
                }else{
                    return '这不是您的电桩,请勿提交！';
                }
            }else{
                return '电桩未找到！';
            }
        }else{
            return '编号重复,请勿重复提交！';
        }
    }

    public function index($param){
        $page = $param['page']??1;
        $status = $param['status']??0;
        $page = ($page-1)*100;
        $where = "";
        if($status){
            $where = "status = '$status'";
        }
        $list = self::where(['user_id'=>$param['user_id']])->where("delete_time is null")->where($where)->order("id desc")->limit($page,100)->select();
        foreach ($list as $k =>&$v){
            $v['images'] = str_replace('|',',',$v['images']);
        }
        return $list;
    }

    public function del($param){
        $find = self::where(['id'=>$param['id']])->where(["user_id"=>$param['user_id']])->find();
        if(empty($find)){
            return '电桩未找到！';
        }else{
            self::where(['id'=>$param['id']])->where(["user_id"=>$param['user_id']])->update(['delete_time'=>date('Y-m-d H:i:s')]);
            Db::table("store_park")->where(['id'=>$find['park_id']])->update(['status'=>1]);
            return 1;
        }
    }


    public function info($param){
        if(!empty($param['no'])){
            if($param['no']!='undefined'){
                $find = self::where(['sn'=>$param['no']])->find();
            }else{
                $find = self::where(['id'=>$param['id']])->find();
            }

        }else{
            $find = self::where(['id'=>$param['id']])->find();
        }
        $param['user_id'] = $param['user_id']??0;
        $arr = [
            '0:00~0:30','0:30~1:00','1:00~1:30','1:30~2:00','2:00~2:30','2:30~3:00','3:00~3:30','3:30~4:00','4:00~4:30','4:30~5:00',
            '5:00~5:30','5:30~6:00','6:00~6:30','6:30~7:00','7:00~7:30','7:30~8:00','8:00~8:30','8:30~9:00',
            '9:00~9:30','9:30~10:00','10:00~10:30','10:30~11:00','11:00~11:30','11:30~12:00','12:00~12:30','12:30~13:00',
            '13:00~13:30','13:30~14:00','14:00~14:30','14:30~15:00','15:00~15:30','15:30~16:00','16:00~16:30','16:30~17:00',
            '17:00~17:30','17:30~18:00','18:00~18:30','18:30~19:00','19:00~19:30','19:30~20:00','20:00~20:30','20:30~21:00',
            '21:00~21:30','21:30~22:00','22:00~22:30','22:30~23:00','23:00~23:30','23:30~24:00'
        ];

        $find['park_name'] = Db::table("store_park")->where(["id"=>$find['park_id']])->value('name');
        $is_collect = Db::table("store_member_collect")->where("type=1")->where(["table_id"=>$find['id']])->where(["user_id"=>$param['user_id']])->find();
        if($is_collect){
            $find['is_collect'] = 1;
        }else{
            $find['is_collect'] = 0;
        }
        $find['station'] = Db::table("store_power_station")->where(["id"=>$find['station_id']])->find();
        $find['images'] = str_replace('|',',',$find['images']);

        $find['price'] = sprintf('%.4f',($find['price']+$find['price2']+$find['price3']+$find['price4']+$find['service_price']+$find['service_price2']+$find['service_price3']+$find['service_price4'])/4);



        $find['park'] = Db::table("store_park")->where(["id"=>$find['park_id']])->find();
        $find['member'] = Db::table("store_member")->where(["id"=>$param['user_id']])->find();



        $find['cost_price1'] = '尖电价：'.sprintf('%.4f',$find['price']).'元/度 <br>服务费：'.sprintf('%.4f',$find['service_price']).'元/度 <br>';
        $cost_time1 = '';
        $find['cost_price2'] = '峰电价：'.sprintf('%.4f',$find['price2']).'元/度 <br>服务费：'.sprintf('%.4f',$find['service_price2']).'元/度 <br>';
        $cost_time2 = '';
        $find['cost_price3'] = '平电价：'.sprintf('%.4f',$find['price3']).'元/度 <br>服务费：'.sprintf('%.4f',$find['service_price3']).'元/度 <br>';
        $cost_time3 = '';
        $find['cost_price4'] = '谷电价：'.sprintf('%.4f',$find['price4']).'元/度 <br>服务费：'.sprintf('%.4f',$find['service_price4']).'元/度 <br>';
        $cost_time4 = '';
        if($find){
            $cost_model = json_decode($find['cost_model'],true);
            foreach ($cost_model as $kk =>&$vv){
                if($vv==1){
                    $cost_time1 .= $arr[$kk].' ';
                }
                if($vv==2){
                    $cost_time2 .= $arr[$kk].' ';
                }
                if($vv==3){
                    $cost_time3 .= $arr[$kk].' ';
                }
                if($vv==4){
                    $cost_time4 .= $arr[$kk].' ';
                }
            }
            $find['cost_price1'] .= ' 时段：'.$cost_time1;
            $find['cost_price2'] .= ' 时段：'.$cost_time2;
            $find['cost_price3'] .= ' 时段：'.$cost_time3;
            $find['cost_price4'] .= ' 时段：'.$cost_time4;
        }

        return $find;
    }


    public function device_collect($param){
        $is_collect = Db::table("store_member_collect")->where("type=1")->where(["user_id"=>$param['user_id'],"table_id"=>$param['id']])->find();
        if($is_collect){
            Db::table("store_member_collect")->where(["user_id"=>$param['user_id'],"table_id"=>$param['id']])->delete();
            return '取消收藏';
        }else{
            Db::table("store_member_collect")->insert([
                'type'=>1,'table_id'=>$param['id'],"user_id"=>$param['user_id'],'add_time'=>date('Y-m-d H:i:s')
            ]);
            return '收藏成功';
        }

    }


}